package com.study.springboot.common.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author wei.zhao
 * @description:
 * @date 2022年03月22日 23:57
 */
@Slf4j
@Component
@Aspect
public class MyAspect {

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Method method = null;
        for (Method method1 : joinPoint.getSignature().getDeclaringType().getMethods()) {
            if (method1.getName().equals(name)) {
                method = method1;
                break;
            }
        }
        MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
        String[] value = annotation.value();
        log.info("注解标注的方法被拦截--->{}", StringUtils.join(value, ","));
    }

    @Pointcut(value = "@annotation(MyAnnotation)")
    public void pointcut() {
    }

}
